<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true">
        <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="addhandle">新增</el-button >
      </el-col>
    </el-row>
    <el-table  class="newClass" v-loading="loading" :data="localLifeTableList">
        <el-table-column label="分类名称" align="center" prop="name" />
        <el-table-column label="排序" align="center" prop="sortNum" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
            <template #default="scope">
                <el-button link type="primary" icon="" @click="handleUpdate(scope.row)">编辑</el-button>
                <el-button link type="primary" icon="" @click="handleDelete(scope.row)">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="分类" v-model="open" width="400">
        <el-form :model="form" label-width="100px">
            <el-form-item label="分类名称" prop="name">
                <el-input
                v-model="form.name"
                placeholder="分类名称"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
            <el-form-item label="排序" prop="sortNum">
                <el-input
                v-model="form.sortNum"
                placeholder="排序"
                type="text"
                style="width: 215px"
                />
            </el-form-item>
        </el-form>
        <template #footer>
            <div class="dialog-footer">
            <el-button :loading="buttonLoading" :disabled="buttonLoading" type="primary" @click="submitForm">确定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </template>
    </el-dialog>
  </div>
</template>

<script setup name="RiversAndLakesClass">
import {ref} from "vue";
import { postClassifyList,addPostClassify,upPostClassify,delPostClassify} from "@/api/business/newMiniProgram";
const localLifeTableList = ref([]);
const loading = ref(true);
const buttonLoading = ref(false);
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
})
const { proxy } = getCurrentInstance();
const open = ref(false);
const form = ref({
});
const total = ref(0);
function addhandle(){
    form.value = {}
    open.value = true
}
function handleUpdate(e){
    form.value = form.value = JSON.parse(JSON.stringify(e))
    open.value = true
}
/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除标题为"' + row.name + '"的分类？').then(function() {
    loading.value = true;
    return delPostClassify(row.id);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}
function submitForm(){
    buttonLoading.value = true
    form.value.type = '江湖'
    form.value.whetherRecommend = '否'
    if(form.value.id){
        upPostClassify(form.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("修改成功");
                getList()
                open.value = false
            }
        }).finally(()=>{
            buttonLoading.value = false
        })
    }else{
        addPostClassify(form.value).then(res=>{
            if(res.code==200){
                proxy.$modal.msgSuccess("新增成功");
                getList()
                open.value = false
            }
        }).finally(()=>{
            buttonLoading.value = false
        })
    }
   
}
/** 查询极客侠后台管理系统本地消耗列表 */
function getList() {
  loading.value = true;
  queryParams.value.type = '江湖'
  postClassifyList(queryParams.value).then(response => {
    localLifeTableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function cancel(e) {
  open.value = false
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
getList();
</script>

